package com.xinke.service.Impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinke.entity.StationBasicInfo;
import com.xinke.entity.StationBasicQuery;
import com.xinke.entity.TOroTollgrantryaction;
import com.xinke.mapper.source.TOroTollgrantryactionMapper;
import com.xinke.mapper.target.StationBasicInfoMapper;
import com.xinke.service.StationBasicInfoService;
import com.xinke.service.TOroTollgrantryactionService;
import com.xinke.utils.HunanCityCodeUtil;
import com.xinke.utils.MyConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 86987
 * @description 针对表【t_oro_tollgrantryaction(收费门架操作表)】的数据库操作Service实现
 * @createDate 2025-08-29 15:50:43
 */
@Service
public class TOroTollgrantryactionServiceImpl extends ServiceImpl<TOroTollgrantryactionMapper, TOroTollgrantryaction>
        implements TOroTollgrantryactionService {

    @Autowired
    private TOroTollgrantryactionMapper toolTollgrantryactionMapper;

    @Autowired
    private StationBasicInfoService stationBasicInfoService;

    @Autowired
    private StationBasicInfoMapper stationBasicInfoMapper;


    /**
     * 更新数据
     */
    @Override
    @Transactional
    public void updateDate() {
        List<StationBasicInfo> list = stationBasicInfoService.list();
        for (StationBasicInfo stationBasicInfo : list) {
            String gczbh = stationBasicInfo.getGCZBH();
            BigDecimal zh = stationBasicInfo.getZH();
            String xzqhdm = stationBasicInfo.getXZQHDM();
            String s = MyConverter.updateGCZBH(gczbh, zh, xzqhdm);
            stationBasicInfo.setGCZBH(s);
        }
        stationBasicInfoService.remove(null);
        stationBasicInfoService.saveBatch(list);
    }

    /**
     * 更新技术等级
     */
    @Override
    @Transactional
    public void updateJSDJ() {
        List<StationBasicQuery> sectionInfo = toolTollgrantryactionMapper.selectSectionIdAndSectionLevel();
        for (StationBasicQuery stationBasicQuery : sectionInfo) {
            stationBasicInfoMapper.updateBySectionId(stationBasicQuery);
        }
    }


    @Override
    public void changeData() {
        //1.获取source数据
        //上行ETC的list
        List<StationBasicQuery> tList1 = toolTollgrantryactionMapper.selectFromTollgrantryactionAndSectionaction("1");
        //下行ETC的list
        List<StationBasicQuery> tList2 = toolTollgrantryactionMapper.selectFromTollgrantryactionAndSectionaction("2");
        //交调站list
        ArrayList<StationBasicInfo> sbiList = new ArrayList<>();


        //2.遍历处理数据
        int i = 0;
        try {
            for (i = 0; i < tList1.size(); i++) {
                StationBasicInfo sbi = new StationBasicInfo();
                //上行ETC
                StationBasicQuery t1 = tList1.get(i);
                //下行ETC
                StationBasicQuery t2 = tList2.get(i);
                //开始写入数据，总条数33
                //1.YEAR 当年年分
                sbi.setYEAR(Integer.valueOf(t1.getEnableTime().substring(0, 4)));
                //todo:2.观测站标识 not null uuid
                sbi.setGCZBS(UUID.randomUUID().toString());
                //3.XZQHDM 行政区划代码
                sbi.setXZQHDM(HunanCityCodeUtil.getCityCode(t1.getArea()));
                //todo:4.机构标识 not null
                //todo:5.路线标识 not nul
                //6.GCZBH 观测站编号
                String gczbh = MyConverter.converterGCZBH(t1.getTollgrantryid(), t1.getPilenumber(), t1.getArea());
                sbi.setGCZBH(gczbh);
                //7.GCZMC 观测站名称
                sbi.setGCZMC(t1.getTollgrantryname());
                //8.GCZLX 观测站类型
                sbi.setGCZLX(t1.getTollgrantrytype().trim().equals("3") || t1.getTollgrantrytype().trim().equals("4") ? "2" : "1");
                //9.GCZDJ 观测站等级
                sbi.setGCZDJ("省");
                //10.ZH 桩号
                sbi.setZH(MyConverter.convertToDecimal(t1.getPilenumber()));
                //11.QDZH 起点桩号
                sbi.setQDZH(MyConverter.convertToDecimal(t1.getStartstakenum()));
                //12.ZIZH 止点桩号
                sbi.setZIZH(MyConverter.convertToDecimal(t1.getEndstakenum()));
                //13.GCLC 观测里程
                sbi.setGCLC(new BigDecimal(t1.getSectionlength()).divide(new BigDecimal(1000), 3, RoundingMode.HALF_UP));
                //14.QDMC 起点名称
                String[] tollgrantrynames = t1.getTollgrantryname().split("-");
                sbi.setQDMC(tollgrantrynames[0]);
                //15.ZDMC 止点名称
                sbi.setZDMC(tollgrantrynames.length == 1 ? tollgrantrynames[0] : tollgrantrynames[1]);
                //16.SXMC 上行名称
                sbi.setSXMC(t1.getTollgrantryname());
                //17.XXMC 下行名称
                sbi.setXXMC(t2.getTollgrantryname());
                //todo:18.调查方法 not null
                //19.CDSL 车道数量
                String lc = t1.getLaneCount();
                if (lc != null) {
                    String[] t1LaneCount = lc.split("\\|");
                    String[] t2LaneCount = t2.getLaneCount().split("\\|");
                    int laneCount = Integer.valueOf(t1LaneCount[0]) + Integer.valueOf(t1LaneCount[1]) + Integer.valueOf(t2LaneCount[0]) + Integer.valueOf(t2LaneCount[1]);
                    sbi.setCDSL(laneCount);
                }
                //todo:20.技术等级高速公路怎么填？ sbi.setJSDJ("1");
                sbi.setJSDJ(t1.getSectionlevel().toString());
                //21.JSDJWY 技术等级唯一
                sbi.setJSDJWY("是");
                //todo:22.SJSD 设计速度 not null
                //todo:23.SYJTL 基准通行能力 not null
                //todo:24.DIMAO 地貌
                //25.GDFS 供电方式
                sbi.setGDFS(t1.getPowersupplymode());
                //todo:26.TXFS 通信方式
                //27.DCRYSL 调查人员数量 default('0')
                //28.JZRQ 建站日期
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                sbi.setJZRQ(LocalDate.parse(t1.getStarttime(), formatter));
                //29.LONGITUDE 经度
                sbi.setLONGITUDE(t1.getLng().setScale(6, RoundingMode.HALF_UP));
                //30.LATITUDE 纬度
                sbi.setLATITUDE(t1.getLat().setScale(6, RoundingMode.HALF_UP));
                //todo:31.ALTITUDE 海拔
                //32.DESC_INFO 备注
                sbi.setDESC_INFO(t1.getRemark());
                //33.tollgrantry_seq
                sbi.setTollgrantrySeq(t1.getSeq());
                //加入list
                sbiList.add(sbi);
            }
        } catch (Exception e) {
            System.out.println(i);
            throw new RuntimeException(e);
        }
        stationBasicInfoService.saveBatch(sbiList);

    }
}




